
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.SkuDao;
import com.sun.showcase.biz.mall.service.SkuService;
import com.sun.showcase.client.domain.mall.Sku;
import com.sun.showcase.client.query.mall.SkuQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
import com.sun.showcase.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("skuService")
@Transactional
public class SkuServiceImpl implements SkuService{
	@Autowired
	private SkuDao skuDao;
	
	public void setSkuDao(SkuDao dao) {
		this.skuDao = dao;
	}

	public DataGrid datagrid(SkuQuery skuQuery) {
		DataGrid j = new DataGrid();
		Pager<Sku> pager  = find(skuQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<SkuQuery> getQuerysFromEntitys(List<Sku> skus) {
		List<SkuQuery> skuQuerys = new ArrayList<SkuQuery>();
		if (skus != null && skus.size() > 0) {
			for (Sku tb : skus) {
				SkuQuery b = new SkuQuery();
				BeanUtils.copyProperties(tb, b);
				skuQuerys.add(b);
			}
		}
		return skuQuerys;
	}

	private Pager<Sku> find(SkuQuery skuQuery) {
		Page<Sku> page = PageHelper.startPage(skuQuery.getPage().intValue(), skuQuery.getRows().intValue());
		List<Sku> list = skuDao.findList(skuQuery);
		Pager<Sku> pager = new Pager<Sku>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public Sku add(SkuQuery skuQuery) {
		Sku t = new Sku();
		BeanUtils.copyProperties(skuQuery, t);
		t.setActiveFlag(StringConstant.ONE);
		t.setCreateBy(LoginContextHolder.get().getUserId());
		t.setCreateByName(LoginContextHolder.get().getUserName());
		t.setCreateDate(new Date());
	    t.setModifiedBy(LoginContextHolder.get().getUserId());
	    t.setModifiedByName(LoginContextHolder.get().getUserName());
	    t.setModifiedDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		skuDao.save(t);
		skuQuery.setId(t.getId());
		return t;
		
	}

	public void update(SkuQuery skuQuery) {
		Sku t = skuDao.getById(skuQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(skuQuery, t);
		    t.setModifiedBy(LoginContextHolder.get().getUserId());
		    t.setModifiedByName(LoginContextHolder.get().getUserName());
		    t.setModifiedDate(new Date());
	    	skuDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(SkuQuery skuQuery) {
	    skuDao.updatePart(skuQuery);
	}

	public void delete(String[] ids) {
		skuDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		skuDao.deletePt(ids);
	}

	public Sku get(SkuQuery skuQuery) {
		return skuDao.getById(skuQuery.getId());
	}

	public Sku get(String id) {
		return skuDao.getById(id);
	}

	
	public List<SkuQuery> listAll(SkuQuery skuQuery) {
	    List<Sku> list = skuDao.findList(skuQuery);
		List<SkuQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public void deleteNotInIds(List<String> ids,String goodsId) {
		skuDao.deleteNotInIds(ids,goodsId);
	}

	@Override
	public void updateStockNum(String id, Integer buyNum) {
		skuDao.updateStockNum(id, buyNum);
	}

	@Override
	public List<Sku> selectNotInIds(List<String> ids, String goodsId) {
		return skuDao.selectNotInIds(ids, goodsId);
	}
	
	
}
